#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* removeElements(struct ListNode* head, int val) {
    if (head == NULL)//1
        return NULL;
    else if (head->next == NULL && head->val == val)//2
    {
        return NULL;
    }
    else if (head->next == NULL && head->val != val)//3
        return head;
    else
    {
        struct ListNode* cur = head;
        struct ListNode* prev = head;
        while (cur)
        {
            if (cur->val == val && head->val != val)//4
            {
                prev->next = cur->next;
                cur = cur->next;
            }
            else if (cur->val == val && head->val == val)//5
            {
                head = head->next;
                cur = head;
                prev = head;
            }
            else//6
            {
                prev = cur;
                cur = cur->next;

            }


        }
    }
    return head;
}